﻿Imports DAO
Imports BUS
Imports DTO

Public Class FormDSSach

    Private flag As Integer = 0
    Private masach As Integer = 0

    Private Sub FormDSSach_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        LoadForm()
        flag = 1
        For Each col As DataGridViewColumn In DataGridViewDS.Columns
            col.SortMode = DataGridViewColumnSortMode.Programmatic
        Next
    End Sub

#Region "Load thông tin lên form"
    Private Sub LoadForm()
        Dim tb As DataTable = Sach_BUS.LaySach()
        DataGridViewDS.DataSource = tb
        DataGridViewDS.Columns(0).Visible = False
        DataGridViewDS.Columns(1).HeaderText = "Tên Sách"
        DataGridViewDS.Columns(2).Visible = False
        DataGridViewDS.Columns(3).HeaderText = "Thể Loại"
        DataGridViewDS.Columns(4).HeaderText = "Tác Giả"
        DataGridViewDS.Columns(5).HeaderText = "Số Lượng Tồn"
        DataGridViewDS.Columns(6).HeaderText = "Đơn Giá"
        DataGridViewDS.Columns(7).Visible = False
        DataGridViewDS.Columns(8).HeaderText = "Tình Trạng"

        tb = TheLoai_BUS.LoadTheLoai()
        ComboBoxTheLoai.DataSource = tb
        ComboBoxTheLoai.ValueMember = "MaTheLoai"
        ComboBoxTheLoai.DisplayMember = "TenTheLoai"

        tb = Sach_BUS.LayTinhTrang()
        ComboBoxTT.DataSource = tb
        ComboBoxTT.ValueMember = "Ma"
        ComboBoxTT.DisplayMember = "Ten"

        ComboBoxTheLoai.SelectedIndex = -1
        ComboBoxTT.SelectedIndex = -1
        TextBoxTen.Clear()
        TextBoxTacGia.Clear()
        TextBoxDG.Clear()
        TextBoxSL.Clear()
        DataGridViewDS.ClearSelection()
    End Sub
#End Region

#Region "Xử lý thay đổi dòng chọn trong datagridview"
    Private Sub DataGridViewDS_SelectionChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DataGridViewDS.SelectionChanged
        If flag = 1 Then
            Dim index As Integer = DataGridViewDS.CurrentRow.Index
            masach = DataGridViewDS.Rows(index).Cells(0).Value.ToString()
            TextBoxTen.Text = DataGridViewDS.Rows(index).Cells(1).Value.ToString()
            ComboBoxTheLoai.SelectedValue = DataGridViewDS.Rows(index).Cells(2).Value.ToString()
            TextBoxTacGia.Text = DataGridViewDS.Rows(index).Cells(4).Value.ToString()
            TextBoxSL.Text = DataGridViewDS.Rows(index).Cells(5).Value.ToString()
            TextBoxDG.Text = DataGridViewDS.Rows(index).Cells(6).Value.ToString()
            ComboBoxTT.SelectedValue = DataGridViewDS.Rows(index).Cells(7).Value.ToString()
        End If
    End Sub
#End Region

#Region "Xử lý nút sửa sách"
    Private Sub ButtonSua_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        If masach > -1 Then
            Dim ten As String = TextBoxTen.Text
            Dim matl As Integer = Integer.Parse(ComboBoxTheLoai.SelectedValue)
            Dim tg As String = TextBoxTacGia.Text
            Dim sl As Integer = Integer.Parse(TextBoxSL.Text)
            Dim dg As Double = Double.Parse(TextBoxDG.Text)
            Dim matt As Integer = Integer.Parse(ComboBoxTT.SelectedValue)
            If Sach_BUS.SuaSach(masach, ten, matl, tg, sl, dg, matt) Then
                MessageBox.Show("Sửa sách thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information)
                LoadForm()
            End If
        Else
            MessageBox.Show("Chưa chọn sách")
        End If
    End Sub
#End Region


    Private Sub ButtonThoat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonThoat.Click
        Me.Close()
    End Sub

#Region "Xử lý nút ghi danh sách sách ra file exel"
    Private Sub ButtonGhi_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonGhi.Click
        Dim browser As New SaveFileDialog()
        browser.Filter = ".xls|*.xls"
        browser.ShowDialog()

        Dim src As String = browser.FileName
        If src <> "" Then
            Dim app As Microsoft.Office.Interop.Excel._Application = New Microsoft.Office.Interop.Excel.Application()
            Dim workbook As Microsoft.Office.Interop.Excel._Workbook = app.Workbooks.Add(Type.Missing)
            Dim worksheet As Microsoft.Office.Interop.Excel._Worksheet = Nothing
            worksheet = workbook.Sheets("Sheet1")
            worksheet = workbook.ActiveSheet
            worksheet.Name = "Danh Sach Sach"

            Dim i, j As Integer
            Dim col As Integer = 1
            For j = 0 To DataGridViewDS.Columns.Count - 1
                Dim r As Microsoft.Office.Interop.Excel.Range = worksheet.Cells(1, col)
                r.Font.Color = System.Drawing.ColorTranslator.ToOle(Color.Blue)
                r.Font.Bold = True
                If DataGridViewDS.Columns(j).Visible = False Then
                    j = j + 1
                End If
                worksheet.Cells(1, col) = DataGridViewDS.Columns(j).HeaderText
                col = col + 1
            Next

            For i = 0 To DataGridViewDS.Rows.Count - 2
                col = 1
                For j = 0 To DataGridViewDS.Columns.Count - 1
                    If DataGridViewDS.Columns(j).Visible = False Then
                        j = j + 1
                    End If
                    worksheet.Cells(i + 2, col) = DataGridViewDS.Rows(i).Cells(j).Value.ToString()
                    col = col + 1
                Next
            Next

            workbook.SaveAs(src, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing)
            MessageBox.Show("Đã ghi file thành công")
            app.Visible = False
            app.Quit()
        End If
    End Sub
#End Region

End Class